package persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import model.Instituicao;
import model.Motorista;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

public class MotoristaDAO implements IDAO {

	private static MotoristaDAO instance = null;
	
	private Session session;
	
	private Transaction transaction;
	
	private MotoristaDAO() {	}
	
	public static MotoristaDAO getInstance() {
		if (instance == null) {
			instance = new MotoristaDAO();
		}
		return instance;
	}

	@Override
	public void alterar(Object objeto) {
		session = HibernateSessionFactory.currentSession();
		transaction = session.beginTransaction();
		try {
			Motorista instituicaoNew = (Motorista) objeto;
			session.saveOrUpdate(instituicaoNew);
			transaction.commit();						
		} catch (Exception e) {			
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	private boolean allEmpty(Motorista m) {
		return isEmptyField(m.getNome()) && 
			   isEmptyField(m.getCpf());		 
	}
	
	private boolean isEmptyField(String field) {
		return field == null || "".equals(field);
	}
	
	public Collection<Object> getMotoristasAccordingTo(Motorista m) {
		Collection<Object> motoristasMatching = new ArrayList<Object>();
		session = HibernateSessionFactory.currentSession();
		transaction = session.beginTransaction();
		
		try {
			if (allEmpty(m)) {
				motoristasMatching = getTodos();
			} else {			
				Criteria c = session.createCriteria(Instituicao.class);
				if (m.getNome() != null) {
					c.add(Expression.like("nome", m.getNome()));
				}				
				if (m.getCpf() != null) {
					c.add(Expression.like("cpf", m.getCpf()));
				}
				motoristasMatching = c.list();
			}
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}		
		
		return motoristasMatching;
	}

	@Override
	public Motorista getPorChavePrimaria(Object chavePrimaria) {
		Motorista motorista = null;
		Session session = HibernateSessionFactory.currentSession();
		Transaction transaction = session.beginTransaction();		
		try{
			Long chave = (Long) chavePrimaria;
			motorista = (Motorista) session.get(Motorista.class, chave);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}		
		return motorista;
	}

	@Override
	public Collection<Object> getTodos() {
		List<Object> motoristas = new ArrayList<Object>();
		session = HibernateSessionFactory.currentSession();
		transaction = session.beginTransaction();
		
		try {
			motoristas = session.createQuery("SELECT b FROM Motorista AS b ORDER BY b.id").list();
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {		
			HibernateSessionFactory.closeSession();
		}

		return motoristas;
	}

	@Override
	public void remover(Object chavePrimaria) {
		session = HibernateSessionFactory.currentSession();
		transaction = session.beginTransaction();
		try {
			Motorista motorista = (Motorista) session.get(Motorista.class, (Long) chavePrimaria);
			session.delete(motorista);			
			transaction.commit();			
		} catch (Exception e) {			
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		
	}

	@Override
	public void salvar(Object motorista) {
		session = HibernateSessionFactory.currentSession();
		transaction = session.beginTransaction();	
		
		try{
			session.save(motorista);
			transaction.commit();			
		} catch (Exception e) {			
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

}
